# %% import
import fuzzywuzzy.fuzz as fz
import numpy as np
import pandas as pd
from utility import *

import os
# %%


class BasicWord:
    def __init__(self, name, count=1) -> None:
        self.name = name
        self.count = count

# most data are stored in dictionary in formal: name->possibility

# apply agent
# the worker of it and owner a usually stable
# a agent may has many other alias


class Agent(BasicWord):
    def __init__(self, name, count=1) -> None:
        super().__init__(name, count=count)
        self.worker = {}
        self.owner = {}
        self.alias = {}
        self.formal_name = name


'''
a worker has a name and an occupation/agent
'''


class Worker:
    def __init__(self, name, count=1) -> None:
        super().__init__(name, count=count)
        self.agent = {}

# def init_model():


# model_bank_dir = 'banks/bank_'
# (agentName,count) in order
# allAgents = pd.read_csv(model_bank_dir + 'agent'+'.csv', index_col=0)
worker_to_agent = np.load('banks/worker_to_agent.npy', allow_pickle=True).item()
# this collection is needed to merged
# agent_to_worker = np.load('banks/agent_to_worker.npy',
                        #   allow_pickle=True).item()


# %%
worker_count = banks_load_for('worker')
alias_build_rule(worker_to_agent,worker_count)
agent_alias_book, alias_agent_book = alias_read_from()
ailias_read_from_manual(agent_alias_book, alias_agent_book)

f = open('log.txt','w')
f.write(str(agent_alias_book).replace('],',']\n'))
f.write(str(alias_agent_book).replace(',','\n'))
f.close()

np.save('banks/agent_alias_book',agent_alias_book)
np.save('banks/alias_agent_book',alias_agent_book)



agents = banks_load_for('agent').to_dict()['count']
agents_counts = statistics_agent_count(agents,alias_agent_book)

f = open('agents_no_alias_count.txt','w')
f.write(str(agents_counts).replace(',','\n').replace('\'','').replace(':',',').replace(' ',''))
f.close()

#%%
# agents_counts = statistics_agent_count(agents,alias_agent_book)

# np.save('banks/agents_merged_count',agents_counts)


agents = banks_load_for('agent')
agents['major'] = agents.index
alias = banks_load_for('alias_agent')  # dic

for a in alias:
    major = alias[a]
    count = agents.loc[major, 'count']
    agents.loc[a] = [count, major]

agents.sort_values('count', ascending=False, inplace=True)
agents.to_csv('banks/agents_count.csv',
                     header='count', index=True)


# %%
